package main

import (
	"context"
	"flag"
	"fmt"
	"math/rand"
	"time"

	slib "gitee.com/xiaoa7/socklib"
)

var (
	serverAddress = flag.String("sa", "127.0.0.1:8080", "服务端地址")
)

func init() {
	flag.Parse()
}

//
func OnRead(data []byte) {
	fmt.Printf("read :: % X %s \n", data, string(data))
}

//
func main() {
	ctx := context.TODO()

	kc := slib.NewKeepConn("tcp", *serverAddress,
		OnRead,
		slib.DefaultDataFrameFactory,
		ctx)
	//一直读
	go kc.Read(ctx)
	//读写使用不同的Frame
	for {
		msg := fmt.Sprintf("你好 A%03d", rand.Intn(100))
		kc.Write([]byte(msg), ctx)
		time.Sleep(time.Second)
	}

}
